import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../res/lf_color.dart';
import '../../res/lf_fonts.dart';
import '../../res/lf_text_style.dart';
import '../../res/strings/lf_strings.dart';
import '../lf_home/lf_home_logic.dart';
import 'lf_faves/lf_faves_view.dart';
import 'lf_liked_me/lf_admires_view.dart';
import 'lf_tab_contact_logic.dart';
import 'lf_visitor/lf_visitor_view.dart';

class LfTabContactPage extends StatefulWidget {
  const LfTabContactPage({super.key});

  @override
  State<LfTabContactPage> createState() => _LfTabContactPageState();
}

class _LfTabContactPageState extends State<LfTabContactPage>
    with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {
  final logic = Get.put(LfTabContactLogic());

  final LfHomeLogic homeLogic = Get.find();
  List<Widget> pages = List.empty(growable: true);
  RxInt currentIndex = 0.obs;
  late TabController controller;
  @override
  void initState() {
    pages.clear();
    pages.add(const LfVisitorPage());
    pages.add(const LfAdmiresPage());
    pages.add(const LfFavesPage());
    controller = TabController(length: 3, vsync: this)
      ..addListener(() {
        currentIndex.value = controller.index;
      });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: ShaderMask(
            shaderCallback: (Rect bounds) {
              return LinearGradient(
                begin: const Alignment(-0.5, -0.9),
                end: const Alignment(0.5, 0.9),
                colors: [colorFF007F, colorFF007F, colorBF00FF, colorBF00FF],
              ).createShader(Offset.zero & bounds.size);
            },
            blendMode: BlendMode.srcATop,
            child: Text(
              'Contacts',
              style: TextStyle(
                  fontSize: 28.sp,
                  fontFamily: fontYoungSerif,
                  letterSpacing: 1.2),
            )),
      ),
      body: DefaultTabController(
          length: pages.length,
          child: Scaffold(
            appBar: TabBar(
              isScrollable: true,
              tabs: [
                Obx(() {
                  bool isSelected = 0 == currentIndex.value;
                  return _tabWidget(
                      title: strVisitors.tr,
                      isSelected: isSelected,
                      num: homeLogic.badgeEntity.value.newVisitedMe);
                }),
                Obx(() {
                  bool isSelected = 1 == currentIndex.value;
                  return _tabWidget(
                      title: strAdmirers.tr,
                      isSelected: isSelected,
                      num: homeLogic.badgeEntity.value.newLikedMe);
                }),
                Obx(() {
                  bool isSelected = 2 == currentIndex.value;
                  return _tabWidget(
                      title: strFaves.tr, isSelected: isSelected, num: 0);
                }),
              ],
              indicator: null,
              indicatorColor: Colors.transparent,
              indicatorPadding: EdgeInsets.zero,
              indicatorSize: TabBarIndicatorSize.tab,
              indicatorWeight: 2.h,
              controller: controller,
            ),
            body: TabBarView(
              controller: controller,
              children: pages,
            ),
          )),
    );
  }

  Widget _tabWidget(
      {required String title, required int num, required bool isSelected}) {
    return Badge.count(
      count: num,
      offset: Offset(20.w, 4),
      textStyle: TextStyle(fontSize: 12.sp, color: Colors.white),
      backgroundColor: colorFF007F,
      textColor: colorFEFEFE,
      isLabelVisible: num > 0,
      child: Text(title,
          style: lfTextStyle(
              sp: 16.sp,
              fontFamily: fontMedium,
              color: isSelected ? colorFF007F : color514F62)),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;
}
